
/**
 * @Title: SgsRepeatCheck.java
 * @Package com.py.credit.clean.code
 * @Description: TODO
 * Copyright: Copyright (c) 2020
 * Company:金电联行(江苏)信息技术有限公司
 * @author Comsys-jdzc
 * @date 2020年9月28日 下午8:51:46
 * @version V1.0
 */

package com.py.credit.clean.code;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.dao.RuleCheckingDao;

/**
 * @ClassName: SgsRepeatCheck
 * @Description: TODO(双公示重复规则校验)
 * @author jdzc
 * @date 2020年9月28日 下午8:51:46
 *
 */

public class SgsRepeatCheck {
	WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
	private RuleCheckingDao ruleCheckingDao = context.getBean(RuleCheckingDao.class);

	public boolean run(Map<String, Object> map, String extend) {
		String tablename = "";
		String column = "";

		if (String.valueOf(map.get("tableId")).equals("210000")) {
			tablename = "t_1000000010001_3ioj4GeZQU";
			column = "XK_WSH";
		} else if (String.valueOf(map.get("tableId")).equals("210001")) {
			tablename = "t_1000000010002_XSni4zSzP0";
			column = "CF_WSH";
		} else if (String.valueOf(map.get("tableId")).equals("210002")) {
			tablename = "t_1000010010003_vVwjDLoDWy";
			column = "XK_WSH";
		} else if (String.valueOf(map.get("tableId")).equals("210003")) {
			tablename = "t_1000010010004_KFPULZpF7D";
			column = "CF_WSH";
		}
		if (String.valueOf(map.get("tableId")).equals("210000")
				|| String.valueOf(map.get("tableId")).equals("210001")) {

			if (StringUtils.isNotBlank(String.valueOf(map.get("A000002")))) {
				if (String.valueOf(map.get("A000002")).equals("00000000000000000X")
						|| String.valueOf(map.get("A000002")).equals("00000000000000000x")) {
					List<Map<String, String>> A000003datas = ruleCheckingDao.SgsCheckType(tablename, "A000003",
							String.valueOf(map.get("A000003")),String.valueOf(map.get("record_id")));
					if (!A000003datas.isEmpty()) {

						for (Map<String, String> A000003data : A000003datas) {

							if (!CollectionUtils.isEmpty(A000003data)) {
								if (String.valueOf(map.get("tableId")).equals("210000")) {
									if (isContains(String.valueOf(map.get("XK_XKWS")))
											|| isContains(String.valueOf(map.get("XK_XKZS")))
											|| isContains(String.valueOf(map.get("XK_NR")))
											|| isContains(String.valueOf(map.get("BZ")))) {
										return true;
									} else if (String.valueOf(map.get("A000001")).equals(A000003data.get("A000001"))
											&& String.valueOf(map.get(column)).equals(A000003data.get(column))) {
										return false;
									} 
								} else {
									if (String.valueOf(map.get("A000001")).equals(A000003data.get("A000001"))
											&& String.valueOf(map.get(column)).equals(A000003data.get(column))) {
										return false;
									} 
								}
							} else {
								return true;
							}

						}

					} else {
						return true;
					}
				} else {
				
					 List<Map<String, String>> A000002datas = ruleCheckingDao.SgsCheckType(tablename, "A000002",
							String.valueOf(map.get("A000002")),String.valueOf(map.get("record_id")));
					if (!A000002datas.isEmpty()) {
						for (Map<String, String> A000002data : A000002datas) {
							if (!CollectionUtils.isEmpty(A000002data)) {
								if (String.valueOf(map.get("tableId")).equals("210000")) {
									if (isContains(String.valueOf(map.get("XK_XKWS")))
											|| isContains(String.valueOf(map.get("XK_XKZS")))
											|| isContains(String.valueOf(map.get("XK_NR")))
											|| isContains(String.valueOf(map.get("BZ")))) {
										return true;
									} else if (String.valueOf(map.get("A000001")).equals(A000002data.get("A000001"))
											&& String.valueOf(map.get(column)).equals(A000002data.get(column))) {
										return false;
									} 
								} else if (String.valueOf(map.get("A000001")).equals(A000002data.get("A000001"))
										&& String.valueOf(map.get(column)).equals(A000002data.get(column))) {
									return false;
								} 
							} else {
								return true;
							}
						}
					} else {
						return true;
					}
				}

			}

		} else if (String.valueOf(map.get("tableId")).equals("210002")
				|| String.valueOf(map.get("tableId")).equals("210003")) {
			// 根据B000003获取主体信息
			List<Map<String, String>> zrrdatas = ruleCheckingDao.SgsCheckType(tablename, "B000002",
					String.valueOf(map.get("B000002")),String.valueOf(map.get("record_id")));
			if (!zrrdatas.isEmpty()) {

				// 判断主体信息是否匹配
				for (Map<String, String> zrrdata : zrrdatas) {

					if (!CollectionUtils.isEmpty(zrrdata)) {
						if (String.valueOf(map.get("tableId")).equals("210002")) {
							if (isContains(String.valueOf(map.get("XK_XKWS")))
									|| isContains(String.valueOf(map.get("XK_XKZS")))
									|| isContains(String.valueOf(map.get("XK_NR")))
									|| isContains(String.valueOf(map.get("BZ")))) {
								return true;
							} else if (String.valueOf(map.get("B000001")).equals(zrrdata.get("B000001"))
									&& String.valueOf(map.get(column)).equals(zrrdata.get(column))) {
								return false;
							} 
						} else if (String.valueOf(map.get("B000001")).equals(zrrdata.get("B000001"))
								&& String.valueOf(map.get(column)).equals(zrrdata.get(column))) {
							return false;
						} 
					} else {
						return true;
					}

				}
			} else {
				return true;
			}
		}

		return true;

	}
	
	//判断数据是否包含注销；变更；延续
	public boolean isContains(String data) {
		if(data.contains("注销")||data.contains("延续")||data.contains("变更")) {
			return true;
		}
		
		return false;
		
	}
	
}
